<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 站点管理相关Controller基础类 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller\site;


use app\core\controller\CoreBaseController;
use app\core\model\SiteConfig;
use think\exception\HttpResponseException;
use think\Session;

class SiteBaseController extends CoreBaseController {
    /**
     * 站点配置
     *
     * @var array
     */
    protected $siteConfig;
    /**
     * 后台登录用户id
     *
     * @var
     */
    protected $siteAdminId;
    /**
     * 后台登录用户信息
     *
     * @var
     */
    protected $siteAdmin;

    /**
     * 构造函数
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function __construct() {
        parent::__construct();

        // 初始化站点配置信息
        $this->_initSiteInfo();

        // 检查站点管理员登录
        $this->_checkInitSiteAdminLogin();
    }

    /**
     * 初始化站点信息
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function _initSiteInfo() {
        $this->siteConfig = SiteConfig::get();
        $this->assign("siteConfig", $this->siteConfig);
    }

    /**
     * 需要登录才能访问，未登录状态时，如果是post访问将提示需要登录，否则将会跳转至登录页面
     */
    protected function _checkInitSiteAdminLogin() {
        $this->siteAdmin = session(SESSION_CORE_SITE_ADMIN);

        if (empty ($this->siteAdmin) || empty ($this->siteAdmin['id'])) {
            if (is_submit()) {
                $this->error("请登录");
            } else {
                // 跳转到登录页面
                Session::set('redirect_url_site', $this->request->url());
                throw new HttpResponseException(
                    redirect('/core/site/login')
                );
            }
        } else {
            $this->siteAdminId = $this->siteAdmin['id'];
        }
        $this->assign("siteAdmin", $this->siteAdmin);
        $this->assign("siteAdminId", $this->siteAdminId);
    }

    /**
     * 检查登录
     */
    protected function _siteAdminLoginRestore() {
        $siteAdmin = session(SESSION_CORE_SITE_ADMIN);
        if (!empty ($siteAdmin) && !empty ($siteAdmin['id'])) {
            $url = Session::get('redirect_url_site');
            Session::delete('redirect_url_site');
            if (empty($url) || in_array($url, ['/core/site/logout', '/core/site/login'])) {
                $url = '/core/site/';
            }
            throw new HttpResponseException(
                redirect($url)
            );
        }
    }

    /**
     * 更新站点管理员session
     *
     * @param $siteAdmin
     */
    protected function _setSiteAdminSession($siteAdmin) {
        session(SESSION_CORE_SITE_ADMIN, $siteAdmin);
        $this->siteAdmin = $siteAdmin;
        $this->siteAdminId = $siteAdmin['id'];
    }

    /**
     * 清理user session
     */
    protected function _clearSiteAdminSession() {
        session(SESSION_CORE_SITE_ADMIN, null);
        $this->siteAdmin = null;
        $this->siteAdminId = null;
    }
}